package com.book.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.book.dao.BookDao;
import com.book.domain.Book;

public class AddbookServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings({ "deprecation", "rawtypes" })
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		long sizeMax = 4 * 1024 * 1024;// 最大上传文件大小为4M
		DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
		diskFileItemFactory.setSizeThreshold(4096);// 设置上传缓存区大小为4K
		diskFileItemFactory.setRepository(new File(req.getRealPath("/")
				+ "upload"));// 设置上传文件目录
		ServletFileUpload servletFileUpload = new ServletFileUpload(
				diskFileItemFactory);
		servletFileUpload.setSizeMax(sizeMax);
		String path;
		String prefix = null;
		String extname = null;
		try {
			List fileitemlistList = servletFileUpload.parseRequest(req); // 解析req对象里的上传数据
			for (Object fileItem : fileitemlistList) {
				FileItem fileItem2 = (FileItem) fileItem;
				if (fileItem2.isFormField()) {
					req.setAttribute(fileItem2.getFieldName(),
							fileItem2.getString("utf-8"));
				} else {
					path = fileItem2.getName();
					String filename = path
							.substring(path.lastIndexOf("\\") + 1);
					 extname = filename.substring(filename
							.lastIndexOf(".") + 1);
					long now=System.currentTimeMillis();
					 prefix=String.valueOf(now);
					String newfilename= req.getRealPath("/")+"upload/"+prefix+"."+extname;
				    try {
						fileItem2.write(new File(newfilename));
						System.out.println(newfilename);
						System.out.println("文件写入成功");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		Book book=new Book();
		book.setBookname((String) req.getAttribute("bookname"));
		book.setAuthorname((String) req.getAttribute("authorname"));
		book.setPublishername((String) req.getAttribute("publishername"));
		book.setBookcatalog((String) req.getAttribute("bookcatalog"));
		book.setBookoutline((String) req.getAttribute("bookoutline"));
		book.setBookprice(Float.parseFloat((String)req.getAttribute("bookprice")) );
		book.setBookimgurl(prefix+"."+extname);
		BookDao bookDao=new BookDao();
		bookDao.addBook(book);
		System.out.println(book);
		resp.getWriter().write("图书插入数据库成功");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}
